package com.sale.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.core.utils.StringUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.api.domain.dto.UnitDto;
import com.sale.system.api.model.MaterialUnitDto;
import com.sale.system.domain.*;
import com.sale.system.domain.dto.*;
import com.sale.system.domain.vo.SysAuxiliaryUnitVo;
import com.sale.system.mapper.*;
import com.sale.system.service.ISysAuxiliaryUnitService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: YSL
 * @Date: 2023/7/14 9:47
 * @Desc: 多计量单位 业务层
 */
@RequiredArgsConstructor
@Service
public class SysAuxiliaryUnitServiceImpl implements ISysAuxiliaryUnitService {

    /**
     * 多计量单位Mapper
     */
    private final SysAuxiliaryUnitMapper sysAuxiliaryUnitMapper;

    /**
     * 辅助单位明细Mapper
     */
    private final SysAuxiliaryUnitDetailMapper sysAuxiliaryUnitDetailMapper;

    /**
     * 单个单位mapper
     */
    private final SysAuxiliaryDataMapper sysAuxiliaryDataMapper;

    /**
     * 成品mapper
     */
    private final MaterialFinishedMapper materialFinishedMapper;

    /**
     * 原料mapper
     */
    private final MaterialRawMapper materialRawMapper;


    /**
     * 新增多计量单位
     *
     * @param sysAuxiliaryUnitVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMultiUnit(SysAuxiliaryUnitVo sysAuxiliaryUnitVo) throws Exception {
        SysAuxiliaryUnit sysAuxiliaryUnit = new SysAuxiliaryUnit();
        BeanUtils.copyProperties(sysAuxiliaryUnitVo, sysAuxiliaryUnit);
        // 新增多计量单位
        int insert = sysAuxiliaryUnitMapper.insert(sysAuxiliaryUnit);
        if (insert > 0) {
            List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetailList = sysAuxiliaryUnitVo.getSysAuxiliaryUnitDetailList();

            int prove = -1;
            List<SysAuxiliaryUnitDetail> list = new ArrayList<>();
            for (SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail : sysAuxiliaryUnitDetailList) {
                if (Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity()) <= prove) {
                    throw new Exception("该辅助" + sysAuxiliaryUnitDetail.getUnitName() + "单位数量" + "不能小于上个辅助单位数量");
                }
                prove = Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity());


                sysAuxiliaryUnitDetail.setAuxiliaryId(sysAuxiliaryUnit.getUnitId());
                list.add(sysAuxiliaryUnitDetail);
            }
            sysAuxiliaryUnitDetailMapper.insertBatch(list);
        } else {
            throw new GlobalException("新增多计量单位失败");
        }
        return insert;
    }

    /**
     * 修改多计量单位
     *
     * @param sysAuxiliaryUnitVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMultiUnit(SysAuxiliaryUnitVo sysAuxiliaryUnitVo) throws Exception {
        SysAuxiliaryUnit sysAuxiliaryUnit = new SysAuxiliaryUnit();
        BeanUtils.copyProperties(sysAuxiliaryUnitVo, sysAuxiliaryUnit);
        // 新增多计量单位
        int modify = sysAuxiliaryUnitMapper.updateById(sysAuxiliaryUnit);
        if (modify > 0) {
            int deleteAuxiliary = sysAuxiliaryUnitDetailMapper.deleteByAuxiliaryUnitId(sysAuxiliaryUnit.getUnitId());
            if (deleteAuxiliary > 0) {

                List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetailList = sysAuxiliaryUnitVo.getSysAuxiliaryUnitDetailList();

                int prove = -1;
                List<SysAuxiliaryUnitDetail> list = new ArrayList<>();
                for (SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail : sysAuxiliaryUnitDetailList) {
                    if (Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity()) <= prove) {
                        throw new Exception("该辅助" + sysAuxiliaryUnitDetail.getUnitName() + "单位数量" + "不能小于上个辅助单位数量");
                    }
                    prove = Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity());

                    sysAuxiliaryUnitDetail.setAuxiliaryId(sysAuxiliaryUnit.getUnitId());
                    list.add(sysAuxiliaryUnitDetail);
                }
                sysAuxiliaryUnitDetailMapper.insertBatch(list);
            }
        } else {
            throw new GlobalException("修改多计量单位失败");
        }
        return modify;
    }

    /**
     * 查询单个多计量单位
     *
     * @param unitId
     * @return
     */
    @Override
    public SysAuxiliaryUnitDateDetailDto getMultiUnit(Long unitId) {
        // 查询多计量单位
        SysAuxiliaryUnitDateDetailDto allByUnitId = sysAuxiliaryUnitMapper.getAllByUnitId(unitId);
        if (allByUnitId != null) {
            // 根据多计量单位id查询明细
            List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectMultiUnitList(allByUnitId.getUnitId());
            allByUnitId.setSysAuxiliaryUnitDetailList(sysAuxiliaryUnitDetails);
        }
        return allByUnitId;
    }

    /**
     * 根据物料编码获取多计量单位
     */
    @Override
    public SysUnitDto getDateByCode(String materialCode) {
        SysUnitDto sysUnitDto = new SysUnitDto();
        //成品
        MaterialFinished materialFinished = materialFinishedMapper.selectMaterialFinishedMaterialCode(materialCode);
        //原料
        MaterialRawDto materialRawDto = materialRawMapper.selectDataByCode(materialCode);

        if (materialFinished != null) {
            //判断该物料是单个单位还是多个单位
            if (materialFinished.getMaterialUnit() != null) {
                //获取单单位
                SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialFinished.getMaterialUnit()));
                List<DetailUnitDto> objects = new ArrayList<>();
                DetailUnitDto detailUnitDto = new DetailUnitDto();
                detailUnitDto.setId(sysAuxiliaryData.getAuxiliaryId());
                detailUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
                objects.add(detailUnitDto);
                sysUnitDto.setSysAuxiliaryUnitDetailList(objects);
            } else {
                //多单位
                List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryId, materialFinished.getMaterialMoreUnit()));
                List<DetailUnitDto> objects = new ArrayList<>();
                for (SysAuxiliaryUnitDetail entity : sysAuxiliaryUnitDetails) {
                    DetailUnitDto detailUnitDto = new DetailUnitDto();
                    detailUnitDto.setId(entity.getAuxiliaryUnitId());
                    detailUnitDto.setUnitName(entity.getUnitName());
                    objects.add(detailUnitDto);
                }
                //获取多单位的基本单位
                DetailUnitDto detailUnitDto = new DetailUnitDto();
                SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda()
                    .eq(SysAuxiliaryUnit::getUnitId, materialFinished.getMaterialMoreUnit()));
                detailUnitDto.setId(sysAuxiliaryUnit.getUnitId());
                detailUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
                objects.add(detailUnitDto);

                sysUnitDto.setSysAuxiliaryUnitDetailList(objects);
            }
            sysUnitDto.setMaterialUnit(materialFinished.getMaterialUnit());
        //    sysUnitDto.setMaterialMoreUnit(materialFinished.getMaterialMoreUnit());

            return sysUnitDto;
        } else {
            //判断该物料是单个单位还是多个单位
            if (materialRawDto.getMaterialUnit() != null) {
                //获取单单位
                SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialRawDto.getMaterialUnit()));
                List<DetailUnitDto> objects = new ArrayList<>();
                DetailUnitDto detailUnitDto = new DetailUnitDto();
                detailUnitDto.setId(sysAuxiliaryData.getAuxiliaryId());
                detailUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
                objects.add(detailUnitDto);
                sysUnitDto.setSysAuxiliaryUnitDetailList(objects);
            } else {
                //多单位
                List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryId, materialRawDto.getMaterialMoreUnit()));
                List<DetailUnitDto> objects = new ArrayList<>();
                for (SysAuxiliaryUnitDetail entity : sysAuxiliaryUnitDetails) {
                    DetailUnitDto detailUnitDto = new DetailUnitDto();
                    detailUnitDto.setId(entity.getAuxiliaryUnitId());
                    detailUnitDto.setUnitName(entity.getUnitName());
                    objects.add(detailUnitDto);
                }
                //获取多单位的基本单位
                DetailUnitDto detailUnitDto = new DetailUnitDto();
                SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda()
                    .eq(SysAuxiliaryUnit::getUnitId, materialRawDto.getMaterialMoreUnit()));
                detailUnitDto.setId(sysAuxiliaryUnit.getUnitId());
                detailUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
                objects.add(detailUnitDto);

                sysUnitDto.setSysAuxiliaryUnitDetailList(objects);
            }
            String raw1 = materialRawDto.getMaterialUnit();
            if (StringUtils.isNotEmpty(raw1)) {
                sysUnitDto.setMaterialUnit(Long.parseLong(materialRawDto.getMaterialUnit()));
            }
            String raw2 = materialRawDto.getMaterialMoreUnit();
            if (StringUtils.isNotEmpty(raw2)) {
                sysUnitDto.setMaterialMoreUnit(Long.parseLong(materialRawDto.getMaterialMoreUnit()));
            }
            return sysUnitDto;
        }
    }

    /**
     * 查询多计量单位列表
     *
     * @param sysAuxiliaryUnitVo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<SysAuxiliaryUnitDateDetailDto> selectMultiUnitDetailList(SysAuxiliaryUnitVo sysAuxiliaryUnitVo, PageQuery pageQuery) {
        Page<SysAuxiliaryUnitDateDetailDto> sysAuxiliaryUnitDateDetailDtoPage = sysAuxiliaryUnitMapper.selectAuxiliaryUnit(pageQuery.build(), sysAuxiliaryUnitVo);
        return TableDataInfo.build(sysAuxiliaryUnitDateDetailDtoPage);
    }


    /**
     * 删除多计量单位明细
     *
     * @param unitId
     * @return
     */
    @Override
    public int deleteMultiUnit(Long unitId) {
        // 删除多计量单位
        int deleteAuxiliary = sysAuxiliaryUnitMapper.deleteById(unitId);
        if (deleteAuxiliary > 0) {
            // 删除多计量单位明细
            sysAuxiliaryUnitDetailMapper.deleteByAuxiliaryUnitId(unitId);
        } else {
            throw new GlobalException("删除多计量单位失败");
        }
        return deleteAuxiliary;
    }

    /**
     * 多计量单位下拉
     *
     * @return
     */
    @Override
    public List<SysTheDropDto> selectMultiMeteringUnitList() {
        return sysAuxiliaryUnitMapper.selectMultiMeteringUnitList();
    }


    /**
     * 根据物料编码获取物料单位（dubbo调用）
     *
     * @param id
     * @return
     */
    @Override
    public MaterialUnitDto getUnitDataByCode(Long id) {

        MaterialUnitDto materialUnitDto = new MaterialUnitDto();

        if (id == null) {
            //查询单位集合
            List<UnitDto> list = sysAuxiliaryUnitMapper.selectMaterialUnitList();
            Map<Long, String> collect = list.stream().collect(Collectors.toMap(UnitDto::getId, UnitDto::getUnitName));
            materialUnitDto.setUnitMap(collect);
            return materialUnitDto;
        }

        //一个单位
        SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, id));
        //多单位(基本单位)
        SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda().eq(SysAuxiliaryUnit::getUnitId, id));
        //多单位
        SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.selectOne(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryUnitId, id));
        if (sysAuxiliaryData != null) {
            materialUnitDto.setUnitQuantity("1");
            materialUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
            return materialUnitDto;
        }
        if (sysAuxiliaryUnit != null) {
            materialUnitDto.setUnitQuantity("1");
            materialUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
            return materialUnitDto;
        }
        if (sysAuxiliaryUnitDetail != null) {
            materialUnitDto.setUnitQuantity(sysAuxiliaryUnitDetail.getUnitQuantity());
            materialUnitDto.setUnitName(sysAuxiliaryUnitDetail.getUnitName());

            materialUnitDto.setType(3);
            return materialUnitDto;
        }
        return materialUnitDto;
    }

    /**
     * 根据id查询多计量单位
     *
     * @param unitId
     * @return
     */
    @Override
    public SysAuxiliaryUnitDateDetailDto getMaterialUnitById(Long unitId) throws Exception {
        return sysAuxiliaryUnitMapper.getMaterialUnitById(unitId);
    }


    /**
     * 多单位换算
     *
     * @return
     */
    @Override
    public MaterialUnitDto selectMaterialUnitList(Long id, Long unitQuantity) {
        // 单位数量
        Long unitTotal = unitQuantity;
        // 查询多计量单位
        SysAuxiliaryUnitDateDetailDto allByUnitId = sysAuxiliaryUnitMapper.getAllByUnitId(id);


        List<MaterialUnitDto> list = new ArrayList<>();
        if (allByUnitId != null) {
            // 根据多计量单位id查询明细
            List<AuxiliaryUnitConversionDto> listAuxiliary = sysAuxiliaryUnitDetailMapper.getMultiUnitById(allByUnitId.getUnitId());
            for (AuxiliaryUnitConversionDto auxiliaryUnitConversionDto : listAuxiliary) {

                // 定义换算单位
                int units = 0;
                // 获取最大辅助单位信息
                SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.selectOne(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryUnitId, auxiliaryUnitConversionDto.getAuxiliaryUnitId()));
                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
                // 比较单位数量和排序后的最大单位数量，进行遍历
                if (unitTotal >= Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity())) {
                    while (unitTotal >= Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity())) {
                        materialUnitDto.setUnitQuantity(String.valueOf(units += 1));
                        unitTotal -= Integer.parseInt(sysAuxiliaryUnitDetail.getUnitQuantity());
                        materialUnitDto.setUnitName(materialUnitDto.getUnitQuantity() + sysAuxiliaryUnitDetail.getUnitName());
                    }
                    list.add(materialUnitDto);
                }
            }
            // 获取剩余数量并拼接基础单位
            if (unitTotal != 0) {
                List<MaterialUnitDto> list1 = new ArrayList<>();
                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
                materialUnitDto.setUnitName(unitTotal + allByUnitId.getAuxiliaryUnitName());
                list1.add(materialUnitDto);
                list.addAll(list1);
            }
        }
        // 拼接多单位
        String join = StringUtils.join(list.stream().map(MaterialUnitDto::getUnitName).toArray(), ",");
        MaterialUnitDto materialUnitDto = new MaterialUnitDto();
        materialUnitDto.setUnitName(join);
        return materialUnitDto;
    }

    /**
     * 多单位换算
     *
     * @return
     */
    @Override
    public MaterialUnitDto selectAtWillMaterialUnitList(Long id, Long unitQuantity) {
        // 单位数量
        Long unitTotal = unitQuantity;
        // 查询多计量单位
//        SysAuxiliaryUnitDateDetailDto allByUnitId = sysAuxiliaryUnitMapper.getAllByUnitId(id);


        List<MaterialUnitDto> list = new ArrayList<>();
//        if (allByUnitId == null) {
            // 根据多计量单位id查询明细
//            SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail1 = sysAuxiliaryUnitDetailMapper.selectOne(new QueryWrapper<SysAuxiliaryUnitDetail>()
//                .lambda().eq(SysAuxiliaryUnitDetail::getMaterialId, id));

//            List<AuxiliaryUnitConversionDto> listAuxiliary = sysAuxiliaryUnitDetailMapper.getMultiUnitById(sysAuxiliaryUnitDetail1.getAuxiliaryId());
            List<AuxiliaryUnitConversionDto> listAuxiliary = sysAuxiliaryUnitDetailMapper.getMultiUnitById(id);
            //截取该多单位之后的多单位数据
            List<AuxiliaryUnitConversionDto> objects = new ArrayList<>();
            int i = 0;
            for (AuxiliaryUnitConversionDto entity : listAuxiliary) {
                if (i > 0) {
                    objects.add(entity);
                }
                if (Objects.equals(id, entity.getMaterialId())) {
                    objects.add(entity);
                    i++;
                }
            }

            for (AuxiliaryUnitConversionDto auxiliaryUnitConversionDto : objects) {
                // 定义换算单位
                int units = 0;
                // 获取最大辅助单位信息
//                SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.selectOne(new QueryWrapper<SysAuxiliaryUnitDetail>()
//                    .lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryUnitId, auxiliaryUnitConversionDto.getAuxiliaryUnitId()));

                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
                // 比较单位数量和排序后的最大单位数量，进行遍历
                if (unitTotal >= Integer.parseInt(auxiliaryUnitConversionDto.getUnitQuantity())) {
                    while (unitTotal >= Integer.parseInt(auxiliaryUnitConversionDto.getUnitQuantity())) {
                        materialUnitDto.setUnitQuantity(String.valueOf(units += 1));
                        unitTotal -= Integer.parseInt(auxiliaryUnitConversionDto.getUnitQuantity());
                        materialUnitDto.setUnitName(materialUnitDto.getUnitQuantity() + auxiliaryUnitConversionDto.getUnitName());
                    }
                    list.add(materialUnitDto);
                }
            }
//        }
        // 获取剩余数量并拼接基础单位
//        if (allByUnitId != null) {
//            List<MaterialUnitDto> list1 = new ArrayList<>();
//            MaterialUnitDto materialUnitDto = new MaterialUnitDto();
//            materialUnitDto.setUnitName(unitTotal + allByUnitId.getAuxiliaryUnitName());
//            list1.add(materialUnitDto);
//            list.addAll(list1);
//        }

        // 拼接多单位
        String join = StringUtils.join(list.stream().map(MaterialUnitDto::getUnitName).toArray(), ",");
        MaterialUnitDto materialUnitDto = new MaterialUnitDto();
        materialUnitDto.setUnitName(join);
        return materialUnitDto;
    }

    /**
     * 根据物料编码查询出单位信息和辅助单位
     *
     * @param materialCode
     * @return
     */
    @Override
    public MaterialUnitDto getUnitByCode(String materialCode) {

        MaterialUnitDto materialUnitDto = new MaterialUnitDto();

        //通过物料编码查出单位
        MaterialFinished materialFinished = materialFinishedMapper.selectOne(new QueryWrapper<MaterialFinished>().lambda().eq(MaterialFinished::getMaterialCode, materialCode));

        MaterialRaw materialRaw = materialRawMapper.selectOne(new QueryWrapper<MaterialRaw>().lambda().eq(MaterialRaw::getMaterialCode, materialCode));


        if (materialFinished != null) {

            //判断是多单位还是单个单位
            if (materialFinished.getMaterialUnit() != null) {
                //一个单位
                SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialFinished.getMaterialUnit()));
                materialUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
            }

            if (materialFinished.getMaterialMoreUnit() != null) {
                //多单位
                //List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.findByAuxiliaryId(materialFinished.getMaterialMoreUnit());

                //查询基本单位
                SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda().eq(SysAuxiliaryUnit::getUnitId, materialFinished.getMaterialMoreUnit()));

                String unit = sysAuxiliaryUnit.getAuxiliaryUnitName();

                List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = new ArrayList<>();
       /*         for (SysAuxiliaryUnitDetail auxiliaryUnitDetail : sysAuxiliaryUnitDetail) {
                    unit = unit + "/" + auxiliaryUnitDetail.getUnitName() + "(" + auxiliaryUnitDetail.getUnitQuantity() + ")";

                    com.sale.system.api.domain.SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail1 = new com.sale.system.api.domain.SysAuxiliaryUnitDetail();
                    BeanUtils.copyProperties(auxiliaryUnitDetail, sysAuxiliaryUnitDetail1);
                    sysAuxiliaryUnitDetails.add(sysAuxiliaryUnitDetail1);
                }*/

                materialUnitDto.setList(sysAuxiliaryUnitDetails);
                materialUnitDto.setAuxiliaryUnit(unit);
                materialUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
            }

        }

        if (materialRaw != null) {

            //判断是多单位还是单个单位
            if (materialRaw.getMaterialUnit() != null) {
                //一个单位
                SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialRaw.getMaterialUnit()));
                if (sysAuxiliaryData != null) {
                    materialUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
                }
            }

            if (materialRaw.getMaterialMoreUnit() != null) {
              //  //多单位
               // List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.findByAuxiliaryId(materialRaw.getMaterialMoreUnit());
                //查询基本单位
                SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda().eq(SysAuxiliaryUnit::getUnitId, materialRaw.getMaterialMoreUnit()));

                String unit = sysAuxiliaryUnit.getAuxiliaryUnitName();

                List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = new ArrayList<>();
         /*       for (SysAuxiliaryUnitDetail auxiliaryUnitDetail : sysAuxiliaryUnitDetail) {
                    unit = unit + "/" + auxiliaryUnitDetail.getUnitName() + "(" + auxiliaryUnitDetail.getUnitQuantity() + ")";
                    com.sale.system.api.domain.SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail1 = new com.sale.system.api.domain.SysAuxiliaryUnitDetail();
                    BeanUtils.copyProperties(auxiliaryUnitDetail, sysAuxiliaryUnitDetail1);
                    sysAuxiliaryUnitDetails.add(sysAuxiliaryUnitDetail1);
                }*/

                materialUnitDto.setList(sysAuxiliaryUnitDetails);
                materialUnitDto.setAuxiliaryUnit(unit);
                materialUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
            }

        }

        return materialUnitDto;

    }

    /**
     * 根据物料编码查询出单位信息和辅助单位(返回map)
     */
    @Override
    public Map<String, MaterialUnitDto> getUnitByCodeToMap() {

        //通过物料编码查出单位
        List<MaterialFinished> materialFinishedList = materialFinishedMapper.selectList();

        List<MaterialRaw> materialRawList = materialRawMapper.selectList();

        Map<String, MaterialUnitDto> map = new HashMap<>();
        if (materialFinishedList.size() != 0) {
            for (MaterialFinished materialFinished : materialFinishedList) {
                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
                //判断是多单位还是单个单位
                if (materialFinished.getMaterialUnit() != null) {
                    //一个单位
                    SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialFinished.getMaterialUnit()));
                    materialUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
                }

                if (materialFinished.getMaterialMoreUnit() != null) {
                    //多单位
                    List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryId, materialFinished.getMaterialMoreUnit()).orderByDesc(SysAuxiliaryUnitDetail::getUnitQuantity));
                    //查询基本单位
                    SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda().eq(SysAuxiliaryUnit::getUnitId, materialFinished.getMaterialMoreUnit()));
                    if (sysAuxiliaryUnit != null) {
                        String unit = sysAuxiliaryUnit.getAuxiliaryUnitName();

                        List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = new ArrayList<>();
                        for (SysAuxiliaryUnitDetail auxiliaryUnitDetail : sysAuxiliaryUnitDetail) {
                            unit = unit + "/" + auxiliaryUnitDetail.getUnitName() + "(" + auxiliaryUnitDetail.getUnitQuantity() + ")";

                            com.sale.system.api.domain.SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail1 = new com.sale.system.api.domain.SysAuxiliaryUnitDetail();
                            BeanUtils.copyProperties(auxiliaryUnitDetail, sysAuxiliaryUnitDetail1);
                            sysAuxiliaryUnitDetails.add(sysAuxiliaryUnitDetail1);
                        }

                        materialUnitDto.setList(sysAuxiliaryUnitDetails);
                        materialUnitDto.setAuxiliaryUnit(unit);
                        materialUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
                    }
                }
                map.put(materialFinished.getMaterialCode(), materialUnitDto);
            }


        }

        if (materialRawList != null) {
            for (MaterialRaw materialRaw : materialRawList) {
                MaterialUnitDto materialUnitDto = new MaterialUnitDto();
                //判断是多单位还是单个单位
                if (materialRaw.getMaterialUnit() != null) {
                    //一个单位
                    SysAuxiliaryData sysAuxiliaryData = sysAuxiliaryDataMapper.selectOne(new QueryWrapper<SysAuxiliaryData>().lambda().eq(SysAuxiliaryData::getAuxiliaryId, materialRaw.getMaterialUnit()));
                    if (sysAuxiliaryData != null) {
                        materialUnitDto.setUnitName(sysAuxiliaryData.getAuxiliaryName());
                    }
                }

                if (materialRaw.getMaterialMoreUnit() != null) {
                    //多单位
                    List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetail = sysAuxiliaryUnitDetailMapper.selectList(new QueryWrapper<SysAuxiliaryUnitDetail>().lambda().eq(SysAuxiliaryUnitDetail::getAuxiliaryId, materialRaw.getMaterialMoreUnit()).orderByDesc(SysAuxiliaryUnitDetail::getUnitQuantity));
                    //查询基本单位
                    SysAuxiliaryUnit sysAuxiliaryUnit = sysAuxiliaryUnitMapper.selectOne(new QueryWrapper<SysAuxiliaryUnit>().lambda().eq(SysAuxiliaryUnit::getUnitId, materialRaw.getMaterialMoreUnit()));

                    String unit = sysAuxiliaryUnit.getAuxiliaryUnitName();

                    List<com.sale.system.api.domain.SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = new ArrayList<>();
                    for (SysAuxiliaryUnitDetail auxiliaryUnitDetail : sysAuxiliaryUnitDetail) {
                        unit = unit + "/" + auxiliaryUnitDetail.getUnitName() + "(" + auxiliaryUnitDetail.getUnitQuantity() + ")";
                        com.sale.system.api.domain.SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail1 = new com.sale.system.api.domain.SysAuxiliaryUnitDetail();
                        BeanUtils.copyProperties(auxiliaryUnitDetail, sysAuxiliaryUnitDetail1);
                        sysAuxiliaryUnitDetails.add(sysAuxiliaryUnitDetail1);
                    }

                    materialUnitDto.setList(sysAuxiliaryUnitDetails);
                    materialUnitDto.setAuxiliaryUnit(unit);
                    materialUnitDto.setUnitName(sysAuxiliaryUnit.getAuxiliaryUnitName());
                }
                map.put(materialRaw.getMaterialCode(), materialUnitDto);
            }
        }
        return map;
    }


    /**
     * 返回多单位map集合(报表模块使用)
     */
    @Override
    public Map<Long, String> getUnitToMap() {
        Map<Long, String> map = new HashMap<>();
        List<SysAuxiliaryUnitDetail> sysAuxiliaryUnitDetails = sysAuxiliaryUnitDetailMapper.selectList();
        for (SysAuxiliaryUnitDetail entity : sysAuxiliaryUnitDetails) {
            map.put(entity.getAuxiliaryUnitId(), entity.getUnitQuantity());
        }
        return map;
    }
}
